package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Flight;
import cn.wolfcode.luowowo.article.domain.FlightDept;
import cn.wolfcode.luowowo.article.service.IFlightDeptService;
import cn.wolfcode.luowowo.article.service.IFlightOrderService;
import cn.wolfcode.luowowo.article.service.IFlightService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 飞机票
 *
 * @author 懿
 */
@Controller
@RequestMapping("flight")
public class FlightController {


    @Reference
    private IFlightDeptService flightDeptService;

    @Reference
    private IFlightService flightService;


    @Reference
    private IFlightOrderService flightOrderService;

    @RequestMapping("")
    public String flight(Model model) {

        List<FlightDept> initialA = flightDeptService.selectInitialA(1L);
        model.addAttribute("initialA", initialA);

        List<FlightDept> initialF = flightDeptService.selectInitialA(2L);
        model.addAttribute("initialF", initialF);
        List<FlightDept> initialK = flightDeptService.selectInitialA(3L);
        model.addAttribute("initialK", initialK);
        List<FlightDept> initialQ = flightDeptService.selectInitialA(4L);
        model.addAttribute("initialQ", initialQ);
        List<FlightDept> initialX = flightDeptService.selectInitialA(5L);
        model.addAttribute("initialX", initialX);

        List<FlightDept> hotFlights = flightDeptService.hotFlights();
        model.addAttribute("hotFlights", hotFlights);

        return "flight/index";
    }


    //机场分组转换
    @RequestMapping("aaa")
    @ResponseBody
    public Object zhuan() {
        flightDeptService.change();
        return "ok";
    }


    /**
     * //search 航班搜索
     *
     * @param orgCity 出发城市代码
     * @param dstCity 到达城市代码
     * @param depTime 出发时间
     * @return
     */
    @RequestMapping("search")
    public String search(String orgCity, String dstCity, String depTime, Model model) {


        List<Flight> flights = flightService.selectArr(orgCity, dstCity);

        Map<String, String> stringObjectMap = new HashMap<>();

        String string = JSON.toJSONString(flights);
        stringObjectMap.put("list", string);


        AjaxResult ajaxResult = new AjaxResult();


        ajaxResult.setSuccess(true);
        ajaxResult.setData(flights);

        model.addAttribute("depTime", depTime);

        model.addAttribute("flights", flights);

        return "flight/add";
    }


    /**
     * 航班订单 现行判断
     *
     * @param id
     * @param depTime
     * @param userInfo
     * @return
     */
    @RequestMapping("buy")
    @ResponseBody
    public Object buy(Long id, String depTime, @UserParam UserInfo userInfo) {
        AjaxResult ajaxResult = new AjaxResult();
        if (userInfo == null) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg("请先登入");
            return ajaxResult;
        }
        if (depTime == null || "".equals(depTime)) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg("请选择出行日期");
            return ajaxResult;
        }
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date parse = sdf.parse(depTime);
            if (parse.getTime() - date.getTime() <= 0) {
                ajaxResult.setSuccess(false);
                ajaxResult.setMsg("你不能选择以前的日期");
                return ajaxResult;
            }


        } catch (ParseException e) {
            e.printStackTrace();
        }


        return ajaxResult;
    }

    /**
     * 航班订单
     *
     * @param id
     * @param time
     * @param userInfo
     * @return
     */
    @RequestMapping("buy1")
    public String buy2(Long id, String time, @UserParam UserInfo userInfo, Model model) {


        Flight flight = flightService.getFilghtById(id);


        //出行时间格式化
        String str = "yyyy-MM-dd";
        SimpleDateFormat sdf1 = new SimpleDateFormat(str);
        Date parse = null;
        try {
            parse = sdf1.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String str2 = "MM月dd日";
        SimpleDateFormat sdf2 = new SimpleDateFormat(str2);
        String format = sdf2.format(parse);
        flight.setChudate(format);

        model.addAttribute("time", time);

        model.addAttribute("flight", flight);


        //批量价格
//        for (int i = 1; i <= 4147; i++) {
//            Random random = new Random();
//            int rannum = (int) (random.nextDouble() * (2999 - 1000 + 1)) + 1000;
//
//
//            flightService.savaPice(i, rannum);
//            System.out.println(rannum);
//        }

        return "flight/buy";
    }


    @RequestMapping("order")
    @ResponseBody
    public Object order(Long id, String time, @UserParam UserInfo userInfo) {


        AjaxResult ajaxResult1 = flightOrderService.saveOrder(id,time, userInfo.getId());
        ajaxResult1.setSuccess(true);
        return ajaxResult1;
    }

}